﻿using FriendAD.Core.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FriendAD.Core.General;
using Microsoft.AspNet.Identity;
using System.Security.Claims;

namespace FriendAD.Core
{
   public class UserManager:BaseManager<User>
    {
        public ClaimsIdentity CreateIdentity(User user, string authenticationType)
        {
            ClaimsIdentity _identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
            _identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            _identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserID.ToString()));
            _identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity"));
            _identity.AddClaim(new Claim("DisplayName", user.DisplayName));
            return _identity;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public override Response Add(User user)
        {
            Response _resp = new Response();
            if (HasAccount(user.UserName))
            {
                _resp.Code = 0;
                _resp.Message = "用户名已存在";
            }
            else
                _resp = base.Add(user);
            return _resp;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userid">主键</param>
        /// <returns></returns>
        public override Response Delete(int userid)
        {
            Response _resp = new Response();
            if (Count() == 1)
            {
                _resp.Code = 0;
                _resp.Message = "不能删除唯一的管理员帐号";
            }
            else _resp = base.Delete(userid);
            return _resp;
        }
        /// <summary>
        /// 删除【批量】返回值Code：1-成功，2-部分删除，0-失败
        /// </summary>
        /// <param name="useridlist"></param>
        /// <returns></returns>
        public Response Delete(List<int> useridlist)
        {
            Response _resp = new Response();
            int _totalDel = useridlist.Count;
            foreach (int i in useridlist)
            {
                    base.Repository.Delete(new User() { UserID=i}, false);
            }
            _resp.Data = base.Repository.Save();
            if (_resp.Data == _totalDel)
            {
                _resp.Code = 1;
                _resp.Message = "成功删除" + _resp.Data + "名用户";
            }
            else if (_resp.Data > 0)
            {
                _resp.Code = 2;
                _resp.Message = "成功删除" + _resp.Data + "名用户";
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = "删除失败";
            }
            return _resp;
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="username">帐号</param>
        /// <param name="password">密码【密文】</param>
        /// <returns>Code:1-成功;2-帐号不存在;3-密码错误</returns>
        public Response Verify(string username, string password)
        {
            Response _resp = new Response();
            var _user = Repository.Find(a => a.UserName == username);
            if (_user == null)
            {
                _resp.Code = 2;
                _resp.Message = "用户不存在或密码错误";
            }
            else if (_user.Password == password)
            {
                _resp.Code = 1;
                _resp.Message = "验证通过";
            }
            else
            {
                _resp.Code = 3;
                _resp.Message = "用户不存在或密码错误";
            }
            return _resp;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userid">主键</param>
        /// <param name="password">新密码【密文】</param>
        /// <returns></returns>
        public Response ChangePassword(int userid, string password)
        {
            Response _resp = new Response();
            var _user = Find(userid);
            if (_user == null)
            {
                _resp.Code = 0;
                _resp.Message = "该主键的管理员不存在";
            }
            else
            {
                _user.Password = password;
                _resp = Update(_user);
            }
            return _resp;
        }
        /// <summary>
        /// 更新登录信息
        /// </summary>
        /// <param name="userid">主键</param>
        /// <param name="ip">IP地址</param>
        /// <param name="time">时间</param>
        /// <returns></returns>
        public Response UpadateLoginInfo(int userid, string ip, DateTime time)
        {
            Response _resp = new Response();
            var _user = Find(userid);
            if (_user == null)
            {
                _resp.Code = 0;
                _resp.Message = "用户不存在";
            }
            else
            {
                _user.LoginIP = ip;
                _user.LoginTime = time;
                _resp = Update(_user);
            }
            return _resp;
        }
        /// <summary>
        /// 账号是否存在
        /// </summary>
        /// <param name="userName">帐号</param>
        /// <returns></returns>
        public bool HasAccount(string userName)
        {
            return base.Repository.IsContains(a => a.UserName.ToUpper() == userName.ToUpper());
        }
    }
}
